package com.bennu.filter;

import com.alibaba.fastjson.JSON;
import com.bennu.common.BaseContext;
import com.bennu.common.R;
import com.sun.jndi.toolkit.url.Uri;
import com.sun.jndi.toolkit.url.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

//检查用户是否已登录
@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    //路径匹配器，支持通配符
    public static final AntPathMatcher PATH_MATCHER=new AntPathMatcher();
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse) servletResponse;
        //获取本次请求的URI
        String requestURI=request.getRequestURI();
        log.info( "拦截到请求："+requestURI );
        //定义不需要处理的路径
        String[] urls=new String[]{
                "/employee/login",
                "/employee/logout",
                "/common/upload",
                "/common/download",
                "/backend/**",
                "/front/**",
                "/user/sendMsg",
                "/user/login"
                //swagger路径
               /* "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"*/


        };

        //判断本次请求是否需要处理
        boolean check = check( urls, requestURI );
        //如果不需要处理直接放行
        if ( check==true ){
            log.info( "本次请求{}不需要处理",requestURI );
            filterChain.doFilter( request,response );
            return;
        }
        //判断登录状态，已经登录，则放行
        if ( request.getSession().getAttribute( "employee" )!=null ){
            log.info( "用户已登录，用户id为：{}",request.getSession().getAttribute( "employee" ) );
            Long empId = (Long) request.getSession().getAttribute( "employee" );
            BaseContext.seCurrentId( empId );
            filterChain.doFilter( request,response );
            return;
        }
        if ( request.getSession().getAttribute( "user" )!=null ){
            log.info( "用户已登录，用户id为：{}",request.getSession().getAttribute( "user" ) );
            Long empId = (Long) request.getSession().getAttribute( "user" );
            BaseContext.seCurrentId( empId );
            filterChain.doFilter( request,response );
            return;
        }
            log.info( "用户未登录" );
            //如果未登录则返回登录,通过输出流给客户端响应数据
            PrintWriter printWriter = response.getWriter();
            printWriter.write( JSON.toJSONString( R.error("NOTLOGIN")) );
            return;




    }
    //判断本次请求路径是否需要处理
    public  boolean check(String[] urls, String requestURI){
        for (String url : urls) {
            boolean match = PATH_MATCHER.match( url, requestURI );
            if ( match==true ){
                return true;
            }
        }
        return false;
    }
}
